package cn.com.czpb.client.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import cn.com.czpb.client.model.jpush.JpushBean;
import cn.com.czpb.client.model.live.LiveItemBean;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.module.live.activity.LiveDetailActivity;
import cn.com.czpb.client.module.news.activity.NewsAlbum_Activity;
import cn.com.czpb.client.module.news.activity.NewsDetailActivity;
import cn.com.czpb.client.module.news.activity.NewsHtmlActivity;
import cn.com.czpb.client.module.news.activity.NewsVideoActivity;
import cn.com.czpb.client.utils.LogUtils;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/20.
 */

public class JpushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String action = intent.getAction();

        if (bundle == null) {
            return;
        }

//        LogUtils.i("onReceive - " + intent.getAction() + ", extras: "
//                + printBundle(bundle));
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {
            String title = "";
            String extra = "";
            for (String key : bundle.keySet()) {
                if (key.equals("cn.jpush.android.ALERT")) {
                    title = bundle.getString(key);
                    continue;
                }
                if (key.equals("cn.jpush.android.EXTRA")) {
                    extra = bundle.getString(key);
                    LogUtils.e("extra-----" + extra);
                }
            }
            JpushBean obj = new Gson().fromJson(extra, JpushBean.class);
            if (null == obj) {
                return;
            }


            String atype = obj.getAtype();
            String rvalue = obj.getRvalue();

try  {
    NewsBean newsBean = new NewsBean();
    newsBean.setNid(obj.getData().getNid());
    newsBean.setTid(obj.getData().getTid());
    newsBean.setType(obj.getData().getType());
    newsBean.setRtype(obj.getData().getRtype());
    newsBean.setVoteflag(obj.getData().getVoteflag());
    newsBean.setRvalue(obj.getData().getRvalue());
    newsBean.setNewsurl(obj.getData().getNewsurl());
    newsBean.setTitle(obj.getData().getTitle());


    Intent mIntent = new Intent();
    mIntent.putExtra("newbean", newsBean);
    mIntent.putExtra("nid", newsBean.getNid());
    mIntent.putExtra("tid", obj.getData().getTid());
    mIntent.putExtra("from", "newsitem");



// 1新闻 2图集 3直播 4专题 5关联新闻 6视频
    if ("1".equals(newsBean.getRtype())) {
        mIntent.putExtra("voteflag", newsBean.getVoteflag());
        mIntent.setClass(context, NewsDetailActivity.class);
    } else if ("2".equals(newsBean.getRtype())) {
        mIntent.putExtra("rvalue", rvalue);
        mIntent.setClass(context, NewsAlbum_Activity.class);
    }else if ("7".equals(newsBean.getRtype())||"3".equals(newsBean.getRtype())||"11".equals(newsBean.getRtype())){
        mIntent.putExtra("url",newsBean.getNewsurl());
        LogUtils.e("url:"+newsBean.getNewsurl());
        mIntent.setClass(context, NewsHtmlActivity.class);
    }
    else if ("4".equals(newsBean.getRtype())) {
        mIntent.putExtra("voteflag", newsBean.getVoteflag());
        mIntent.setClass(context, NewsDetailActivity.class);
    }else if("6".equals(newsBean.getRtype())){
        mIntent.putExtra("rvalue", rvalue);
        mIntent.putExtra("nid", newsBean.getNid());
        mIntent.setClass(context, NewsVideoActivity.class);
    }
            else if ("14".equals(newsBean.getRtype())) {



                LiveItemBean bean = new LiveItemBean();
        bean.setId(newsBean.getRvalue());
        mIntent.putExtra("bean",bean);
                mIntent.setClass(context, LiveDetailActivity.class);
            }
    else {
        mIntent.setClass(context, NewsDetailActivity.class);
    }
    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
    context.startActivity(mIntent);

}catch (Exception e){
    e.printStackTrace();
}
        }
    }


    // 打印所有的 intent extra 数据
//    private String printBundle(Bundle bundle) {
//        StringBuilder sb = new StringBuilder();
//        String datd = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//        for (String key : bundle.keySet()) {
//            LogUtils.i("keyset-->"+key);
//
//
////            String data = bundle.getString(key);
//            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
//                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
//            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
//                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
//
//            } else {
//                if ("cn.jpush.android.NOTIFICATION_ID".equals(key)) {
//                    sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
//                } else {
//                    sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
//                }
//            }
//        }
//        return sb.toString();
//    }
}
